package com.jjyu.controller;

import com.jjyu.entity.PointsLog;
import com.jjyu.entity.Result;
import com.jjyu.service.PointsLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/points")
public class PointsController {

    @Autowired
    private PointsLogService pointsLogService;

    @GetMapping("/total/{userId}")
    public Result<Integer> getTotalPointsByUserId(@PathVariable int userId) {
        int totalPoints = pointsLogService.getTotalPointsByUserId(userId);
        return Result.success(totalPoints);
    }

    @PostMapping("/add")
    public Result<PointsLog> addPointsLog(@RequestBody PointsLog pointsLog) {
        boolean result = pointsLogService.save(pointsLog);
        if (result) {
            return Result.success(pointsLog);
        } else {
            return Result.error("添加积分记录失败");
        }
    }


    @DeleteMapping("/delete/{id}")
    public Result<String> deletePointsLog(@PathVariable Long id) {
        boolean result = pointsLogService.removeById(id);
        if (result) {
            return Result.success("删除积分记录成功");
        } else {
            return Result.error("删除积分记录失败");
        }
    }
}



